package org.zxt.web.context.handler.parser;

import org.zxt.web.context.WebContext;
import org.zxt.web.context.annotation.RequestHeader;
import org.zxt.web.context.annotation.RequestUri;
import org.zxt.web.context.handler.ArgumentParser;
import org.zxt.web.nio.IHTTPSession;

import javax.annotation.Resource;
import java.lang.reflect.Parameter;

public class RequestHeaderParser implements ArgumentParser {
    @Override
    public boolean support(Parameter p) {
        return p.getAnnotation(RequestHeader.class) != null;
    }

    @Override
    public Object parse(Parameter p, WebContext webContext, IHTTPSession session) {
        RequestHeader requestHeader = p.getAnnotation(RequestHeader.class);
        String val = session.getHeaders().get(requestHeader.value());
        if(p.getType() == Integer.class){
            return Integer.valueOf(val);
        } else if(p.getType() == int.class) {
            return Integer.parseInt(val);
        } else {
            return val;
        }
    }
}
